package com.iisageek.client.widgets.sortabletable;

import org.gwtwidgets.client.ui.PNGImage;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.iisageek.client.lnl;
import com.iisageek.client.Member;

public class OfficersTable extends SpecializedTable implements TableListener {

	public void add(Object element) {
		RowFormatter rowFormatter = table.getRowFormatter();
		rowFormatter.setStyleName(0, "tableHeader");
		CellFormatter cellFormatter = table.getCellFormatter();
		
		table.setValue(table.size()+1, 0, ((Member) element).getLastName() + ", " + ((Member) element).getLastName(), element);
		table.setValue(table.size(), 1, new Integer(((Member) element).getYear()));
		table.setValue(table.size(), 2, ((Member) element).getJoin());
		table.setValue(table.size(), 3, ((Member) element).getStatus());
		
		if (table.size()+1 % 2 == 0) {
			rowFormatter.setStyleName(table.size(), "customRowStyle");
		} else {
			rowFormatter.setStyleName(table.size(), "tableRow");
		}
		for (int colIndex = 0; colIndex < table.getCellCount(0); colIndex++) {
			cellFormatter.setStyleName(table.size(), colIndex, "customFont");
			cellFormatter.setAlignment(table.size(), colIndex,
					HasHorizontalAlignment.ALIGN_LEFT,
					HasVerticalAlignment.ALIGN_MIDDLE);

		}
		
	}

	public void tablePopup(SourcesTableEvents sender, int row, int cell) {
		if (row > 0) {
			MemberPopup popup = new MemberPopup((Member) table.getData(row));
			int top = ((table.getOffsetHeight() / table.getRowCount()) * (row));
			int left = table.getAbsoluteLeft()
					+ ((table.getOffsetWidth() / table.getCellCount(row)) * (cell + 1));
			popup.setPopupPosition(left, top);
			popup.show();
		}
	}

	public void init() {
		table.addColumnHeader("Name", 0);
		table.addColumnHeader("Year", 1);
		table.addColumnHeader("Join Date", 2);
		table.addColumnHeader("Position", 3);
	}
	
	public class MemberPopup extends PopupPanel  implements ClickListener {

		private Member member;
		
		public MemberPopup(Member member) {
			// The popup's constructor's argument is a boolean specifying that
			// it
			// auto-close itself when the user clicks outside of it.
			super(true);

			this.member = member;
			
			VerticalPanel inner = new VerticalPanel();
			Label nameLabel = new Label(member.getLastName() + ", " + member.getLastName());
			Label emailLabel = new Label(member.getEmail());
			Hyperlink profileLink = new Hyperlink("Edit Profile", member.getWPIID());
			
			inner.add(nameLabel);
			inner.add(emailLabel);
			inner.add(profileLink);

			profileLink.addClickListener(this);
			
			HorizontalPanel panel = new HorizontalPanel();
			panel.setSpacing(4);
			panel.add(new PNGImage(member.getPhoto(),64,64));
			panel.add(inner);

			add(panel);
			setStyleName("mail-ContactPopup");
			nameLabel.setStyleName("mail-ContactPopupName");
			emailLabel.setStyleName("mail-ContactPopupEmail");
			profileLink.setStyleName("mail-ContactPopupEmail");
		}

		public void onClick(Widget sender) {
			Hyperlink tmp = (Hyperlink) sender;
			super.setVisible(false);
			Window.alert("Edit: " + member.getFirstName());
		}
	}
}